﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PSE.Framework.ErrorLogging;
using System.Xml.Serialization;
using System.Xml;

namespace Sinacor.Infra.Common.ErrorLogging
{
    public abstract class DbEnvironmentData : EnvironmentData
    {
        [XmlElement]
        public DbEnvironment DbEnvironment { get; set; }
    }

    [Serializable]
    public class DbEnvironment
    {
        [XmlAttribute]
        public string DataSource { get; set; }
        [XmlAttribute]
        public string UserId { get; set; }
        [XmlAttribute]
        public string CommandType { get; set; }
        [XmlAttribute]
        public string CommandText { get; set; }
        [XmlArray("Parameters")]
        [XmlArrayItem("Parameter")]
        public List<string> Parameters { get; set; }

        public override string ToString()
        {
            StringBuilder logData = new StringBuilder();

            logData.AppendLine();
            logData.AppendLine("DataBase Information:");
            logData.AppendLine(LogData.SEPARATOR);

            if (!string.IsNullOrEmpty(this.DataSource))
            {
                logData.AppendFormat("DataSource:\t{0}", this.DataSource);
                logData.AppendLine();
            }

            if (!string.IsNullOrEmpty(this.UserId))
            {
                logData.AppendFormat("UserId:\t{0}", this.UserId);
                logData.AppendLine();
            }

            if (!string.IsNullOrEmpty(this.CommandType))
            {
                logData.AppendFormat("CommandType:\t{0}", this.CommandType);
                logData.AppendLine();
            }

            if (!string.IsNullOrEmpty(this.CommandText))
            {
                logData.AppendFormat("CommandText:\t{0}", this.CommandText);
                logData.AppendLine();
            }

            if (this.Parameters != null && this.Parameters.Count > 0)
            {
                logData.AppendLine("Parameters:");
                logData.AppendLine(string.Join(Environment.NewLine, this.Parameters.ToArray()));
            }

            return logData.ToString();
        }

        public void WriteTo(XmlWriter writer)
        {
            if (!string.IsNullOrEmpty(this.DataSource))
            {
                writer.WriteElementString("DataSource", this.DataSource);
            }

            if (!string.IsNullOrEmpty(this.UserId))
            {
                writer.WriteElementString("UserId", this.UserId);
            }

            if (!string.IsNullOrEmpty(this.CommandType))
            {
                writer.WriteElementString("CommandType", this.CommandType);
            }

            if (!string.IsNullOrEmpty(this.CommandText))
            {
                writer.WriteElementString("CommandText", this.CommandText);
            }

            if (this.Parameters != null && this.Parameters.Count > 0)
            {
                writer.WriteStartElement("Parameters");

                foreach (string parameter in this.Parameters)
                    writer.WriteElementString("Parameter", parameter);

                writer.WriteEndElement();
            }
        }
    }   
}
